function getHttp(a, data, callback) {
	$.ajax({
		url: sdk.baseIP + a,
		data: data,
		dataType: 'jsonp',
		success: function(d, r, x) {
			callback(d, r, x);
		}
	});
}

function toBack() {
	window.history.back();
}

layui.use(['form', 'layer', 'element'], function() {
	var form = layui.form;
	var layer = layui.layer;
	var element = layui.element;
	// 监听底部下拉选择
	form.on('select(urlTo)', function(data) {
		console.log(data);
		if (data.value) {
			window.open(data.value)
		}
	});
	// 改变验证码
	function changeValidateImg() {
		$('#imgYzm').attr('src', '').attr('src', sdk.companyIp +
			'zxw/company/login/getRandomValidateCode?r=' + Math.random());
		form.val("formVcyk", {
			validateCode: ''
		});
	};
	// 登录渲染角色类型
	function loginMain(typeId) {
		if (typeId == 'qy') {
			$('#bklwxo').text('企业登录');
			$('#zwrkoe').show();
		} else if (typeId == 'jg') {
			$('#bklwxo').text('机构登录');
			$('#zwrkoe').hide();
		} else if (typeId == 'zf') {
			$('#bklwxo').text('监管登录');
			$('#zwrkoe').hide();
		}
		layer.open({
			type: 1,
			title: false,
			shadeClose: true,
			content: $('#layerVcyk'),
			success: function(layero, index) {
				changeValidateImg();
				$('#imgYzm').click(function() {
					changeValidateImg()
				});
				form.on('submit(formGohk)', function(d) {
					var rjzpIndex = layer.load(2);
					var data = d.field;
					var acc = data.account;
					var psw = data.password;
					data.account = btoa(acc);
					data.password = btoa(psw);
					setTimeout(function() {
						layer.close(rjzpIndex);
					}, 5000)
					$.ajax({
						type: 'post',
						data: data,
						url: sdk.companyIp +
							'zxw/company/login/loginCheck',
						success: function(d) {
							layer.close(rjzpIndex);
							consoleMain();
							changeValidateImg();
							if (d.code == '00') {
								layer.msg(d.msg, {
									icon: 6,
									closeBtn: 1,
									time: 3000
								});
								if (sdk.user[d.typeId]) {
									location.href = sdk.baseIP + sdk.user[d
										.typeId].path;
								}
								layer.close(index);
							} else {
								layer.msg(d.msg, {
									icon: 2,
									closeBtn: 1,
									time: 3000
								});
							}
						},
						error: function(err) {
							layer.msg('系统错误,请联系管理员!', {
								icon: 5
							});
						}
					});
				});
			},
			end: function() {
				form.val("formVcyk", {
					account: '',
					password: '',
					validateCode: ''
				});
			}
		});
	};
	// 渲染登录页面
	function loginSvkc(typeId) {

		if ($('#layerVcyk').length == 0) {
			
			$.ajax({
				url: 'login2.html',
				dataType: 'html',
				success: function(html) {
					$('body').append(html);
					loginMain(typeId);
				}
			});
		} else {
			loginMain(typeId)
		}
	};
	// 角色身份选择
	element.on('nav(loginUser)', function(elem) {
		var code = $(elem.context).attr('code');
		if (code) {
			loginSvkc(code);
		}
	});

	// 跳转注册
	$('#toRegister').click(function() {
		location.href = sdk.companyIp + 'mh/register.html'
	})
	form.verify({
		pass: [/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$/, '密码必须由6到18位字母和数字组成']
	});
	// 格式化输入文本
	function filterHTMLTag(msg) {
		var msg = msg.replace(/<[^>]+>|&[^>]+;/g, ''); //去除HTML Tag
		msg = msg.replace(/[|]*\n/, '') //去除行尾空格
		msg = msg.replace(/&npsp;/ig, ''); //去掉npsp
		return msg;
	}
	$('input,textarea').on('input', function() {
		$(this).val(filterHTMLTag($(this).val()))
	});
	// 根据地址参数触发修改密码
	if (sdk.getURLVar('type') === 'mpwd') {
		changePassword();
		history.pushState('', '', 'index.html');
	}

	// 修改密码
	function changePassword() {
		$.ajax({
			url: 'modify_pwd.html',
			dataType: 'html',
			success: function(d) {
				$('body').append(d);
				layer.open({
					type: 1,
					title: false,
					shadeClose: true,
					content: $('#nhnvve'),
					success: function(layero, index) {
						form.on('submit(formTwlh)', function(d) {
							var data = d.field;
							if (data.oldPassword === data.newPassword) {
								layer.msg('新密码和旧密码不能相同', {
									icon: 5
								});
								return false;
							}
							if (data.newPassword !== data.newPasswordRepeat) {
								layer.msg('两次输入的密码不一致', {
									icon: 5
								});
								return false;
							}
							data.oldPassword = btoa(data.oldPassword);
							data.newPassword = btoa(data.newPassword);
							$.ajax({
								type: 'post',
								data: data,
								url: sdk.baseIP +
									'zxw/company/core/changePassword',
								success: function(d) {
									if (d.code === '00') {
										layer.close(index);
										layer.msg('修改成功', {
											icon: 6,
											closeBtn: 1,
											time: 3000
										});
										location.href = sdk.baseIP +
											'zxw/company/login/loginout'
									} else {
										layer.msg(d.msg, {
											icon: 5,
											closeBtn: 1,
											time: 3000
										});
									}
								},
								error: function(err) {
									layer.msg('系统错误,请联系管理员!', {
										icon: 5
									});
								}
							})
						})
					},
					end: function() {
						form.val('formPyzj', {
							oldPassword: '',
							newPassword: '',
							newPasswordRepeat: ''
						})
					}
				})
			}
		})
	};

	// 跳转到工作台
	$('.bankName').click(function() {
		var typeId = $(this).attr('data-val');
		if (sdk.user[typeId]) {
			location.href = sdk.baseIP + sdk.user[typeId].path;
		}
	});

	// 签退
	$('.logoutBtn').click(function() {
		var layer = layui.layer;
		layer.confirm('你确定要退出吗?', {
			icon: 0,
			title: '提示'
		}, function(index) {
			layer.close(index);
			sessionStorage.setItem('baohan_info', '');
			location.href = sdk.baseIP + 'zxw/company/login/loginout';
		});
	});

	// 个人信息
	function consoleMain() {
		$.ajax({
			type: 'post',
			url: sdk.companyIp + 'zxw/company/login/getLoginFlag',
			dataType: 'json',
			success: function(userinfo) {
				if (userinfo.loginFlag == 'true') {
					$('#isLogin').removeClass('noShow');
					$('#noLogin').addClass('noShow');
					$('.bankName').text(userinfo.userName);
					$('.bankName').attr('data-val', userinfo.userType);
				} else {
					$('#noLogin').removeClass('noShow');
					$('#isLogin').addClass('noShow');
				}
			},
			error: function(err) {
				layer.msg('系统错误,请联系管理员!', {
					icon: 5
				});
				$('#noLogin').removeClass('noShow');
				$('#isLogin').addClass('noShow');
			}
		});
	}
	consoleMain();

	// 查看手册
	var times;
	$('.nav_body').click(function() {
		var isDisplay = $('.file_body').css('display');
		if (isDisplay == 'none') {
			$('.file_body').show();
			clearTimeout(times);
			times = setTimeout(function() {
				$('.file_body').hide();
			}, 8000);
		} else if (isDisplay != 'none') {
			$('.file_body').hide();
		}
	});

	$('.file_item').click(function() {
		window.open(sdk.baseIP + 'zxw/index/index/doc/' + $(this).text());
	})
});
